<?php

namespace app\controllers\admin\uni\order;

use app\models\group\Group;
use app\models\group\GroupFans;
use app\models\system\SetExpressCompany;
use app\models\user\User;

/**
 * Class GroupController
 * @package app\controllers\admin\uni\order\
 */
class GroupController extends BaseController
{
    public function actionList()
    {
        $id = $this->post('id');

        $status = $this->post('status', -1);

        $export = $this->post('export');

        $page = intval($this->post('page', $this->page));
        $page_size = intval($this->post('pageSize', $this->pageSize));

        $sort = $this->post('sort');

        // user
        $user_id = $this->post('user_id');
        $fans_id = $this->post('fans_id');

        // date
        $start = $this->post('start');
        $end = $this->post('end');

        $query = Group::find();

        if ($start) {
            $query->andWhere(['>=', 'create_time', $start]);
        }
        if ($end) {
            $query->andWhere(['<=', 'create_time', $end]);
        }

        if ($user_id) {
            $query->andWhere(['user_id' => $user_id]);
        }

        if ($fans_id) {
            $ids = GroupFans::find()
                ->where(['user_id' => $fans_id])
                ->select(['group_id'])
                ->column();
            $query->andWhere(['id' => $ids]);
        }

        if ($status > -1) {
            $query->andWhere(['status' => $status]);
        }

        if ($sort) {
            $prop = substr($sort, 0, 1);
            $prop = $prop == '-' ? SORT_DESC : SORT_ASC;
            $order = substr($sort, 1);
            $query->orderBy([$order => $prop]);
        } else {
            $query->orderBy(['id' => SORT_DESC]);
        }

        if ($id) {
            $query->andWhere(['id' => $id]);
        }

        $with = [
            'product', 'product.mainImage',
            'fans'
        ];

        // export
        if ($export) {
            return $this->success(
                $query->with($with)->asArray()->all()
            );
        }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $page_size);

        $list = $query->offset(($page - 1) * $page_size)
            ->limit($page_size)
            ->with($with)
            ->asArray()
            ->all();

        return $this->success([
            $this->totalCount   => $totalCount,
            $this->totalPage    => $totalPage,
            $this->list         => $list,
        ]);

    }

    public function actionView()
    {
        $id = $this->post('id');

        $model = Group::find()
            ->where(['id' => $id])
            ->with([
                'order', 'order.item', 'order.item.product', 'order.item.product.mainImage',
                // 'product', 'product.mainImage',
                'user',
                'fans', 'fans.user',
                'commission'
            ])
            ->asArray()
            ->one();

        if ($model) {
            return $this->success([
                'group' => $model,
                'item' => $model['order']['item'][0]
            ]);
        }

        return $this->fail('不存在该拼团');
    }
}